//
//  BKMyOrderSplitToPayController.m
//  bestkeep
//
//  Created by Mr. Wu on 16/12/22.
//  Copyright © 2016年 utouu. All rights reserved.
//

#import "BKMyOrderSplitToPayController.h"
#import "BK_OD_Tool.h"
@interface BKMyOrderSplitToPayController ()
@property (nonatomic, strong) UILabel *balance;
@end

@implementation BKMyOrderSplitToPayController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = RGB(221, 221, 221);
    self.title = @"确认支付";
    [self initView];
    [self viplevel];
}

-(void)viplevel{
//    [HUD showProgress:@"正在支付..."];
    [HUD showProgress:nil];
    [BKMyOrderGetDataFromNetWork accountMoneyInfoSuccess:^(id object) {
        [HUD dismiss];
        NSDictionary *dict =object;
        NSArray *arr = dict[@"account"];
        for (NSDictionary *dic in arr) {
            if ([dic[@"account_type"] isEqualToString:@"002"]) {
                self.balance.text = [[NSString stringWithFormat:@"  账户余额：%@元",dic[@"account_balance"] ==nil?@"0.00":dic[@"account_balance"]]description];
            }
        }
    } failure:^(NSError *error) {
        [HUD dismiss];
        if (error.code==502) {
            [HUD showErrorMessage:@"服务器开小差了，请稍后再试！"];
        }else{
            [HUD showErrorMessage:error.localizedDescription];
        }
    }];
    
   }

- (void)initView {
    
    UILabel *order = [[UILabel alloc] init];
    order.backgroundColor = [UIColor whiteColor];
    order.text = @"  共计1笔订单";
    order.font = [UIFont systemFontOfSize:13];
    [self.view addSubview:order];
    [order mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left);
        make.height.equalTo(@44);
        make.width.equalTo(@(ScreenWidth - 60));
        make.top.equalTo(self.view.mas_top).offset(64);
    }];
    
    UILabel *price = [[UILabel alloc] init];
    price.backgroundColor = [UIColor whiteColor];
    price.font = [UIFont systemFontOfSize:13];
    price.text = [NSString stringWithFormat:@"%@元", self.money];
    price.textColor = [UIColor redColor];
    [self.view addSubview:price];
    [price mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(order.mas_right);
        make.height.equalTo(@44);
        make.right.equalTo(self.view.mas_right);
        make.top.equalTo(self.view.mas_top).offset(64);
    }];
    
    _balance = [[UILabel alloc] init];
    _balance.backgroundColor = [UIColor whiteColor];
    _balance.font = [UIFont systemFontOfSize:13];
    
    [self.view addSubview:_balance];
    [_balance mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left);
        make.height.equalTo(@44);
        make.right.equalTo(self.view.mas_right);
        make.top.equalTo(order.mas_bottom).offset(20);
    }];
    UIButton *confirmBtn = [[UIButton alloc] init];
    [self.view addSubview:confirmBtn];
    confirmBtn.backgroundColor = [UIColor greenColor];
    [confirmBtn setTitle:@"确认支付" forState:UIControlStateNormal];
    [confirmBtn setTintColor:[UIColor whiteColor]];
    confirmBtn.backgroundColor = RGB(27, 188, 155);
    [confirmBtn addTarget:self action:@selector(payPost:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:confirmBtn];
    [confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@44);
        make.top.equalTo(_balance.mas_bottom).offset(30);
        make.left.equalTo(self.view.mas_left).offset(20);
        make.right.equalTo(self.view.mas_right).offset(-20);
    }];
}

//拆分单邮费支付
-(void)payPost:(UIButton*)sender{
    
    NSMutableDictionary *splitPay = [NSMutableDictionary dictionary];
    splitPay[@"order_no"] = self.dic[@"order_no"];
    splitPay[@"account_type"] = [NSString stringWithFormat:@"002"];
    splitPay[@"order_item_ids"] = self.dic[@"itemIds"];
//    [HUD showProgress:@"正在支付..."];
    [HUD showProgress:nil];
    [BKMyOrderGetDataFromNetWork splitToPayPostageWithDic:splitPay Success:^(NSString *str) {
        [HUD showSuccessMessage:@"支付成功"];
        [self.navigationController popToRootViewControllerAnimated:YES];
    } failure:^(NSError *error) {
        if (error.code==502) {
            [HUD showErrorMessage:@"服务器开小差了，请稍后再试！"];
        }else{
            [HUD showErrorMessage:error.localizedDescription];
        }
    }];
    
}

@end
